require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#myOrder",
    data:{
        tabList:[
            {val:0,name:"全部"},
            {val:1,name:"待支付"},
            {val:100,name:"待分享"},
            {val:3,name:"待收货"},
            {val:7,name:"已完成"},
            {val:8,name:"已取消"}],
        value:0,
        uid:userInfo.id,
        // uid:1,
        pageNumber:1,
        orderList:{},
        orderList1:[],
        orderList2:[],
        loadFlag:false
    },
    mounted:function(){
        this.userOrderList();
    },
    methods:{
        //后退
        getBack:function(){
            location.href="/happyi";
            // history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //订单列表
        userOrderList:function(){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                pageNumber:that.pageNumber,
                type:that.value
            };
            this.ajax({url:"/purchase/userOrderList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.orderList=b.json.data;
                    that.orderList1=that.orderList.list;
                    if(that.orderList.pageNumber>1){
                        if(that.orderList.list.length>0) {
                            that.orderList2 = that.orderList2.concat(b.json.data.list);
                        }
                    }else{
                        that.orderList2= that.orderList1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.orderList={};
                    that.orderList1=[];
                    that.orderList2=[];
                    vm.$refs.prompt.toast("获取订单列表失败！");
                }
            });
        },
        getTab:function(val){
            this.value=val;
            this.pageNumber=1;
            this.orderList={};
            this.orderList1=[];
            this.orderList2=[];
            this.userOrderList();
        },
        compareDate:function(item){
            var that=this;
            var second=getCompareDate(item.currentTime,item.limitTime,"second");
            return second;
        },
        //时分秒倒计时
        leftTimer:function(item,index){
            var that=this;
            that.$nextTick(function(){
                var second=getCompareDate(item.currentTime,item.limitTime,"second");
                addTimer("stateData"+index,second);
            });
        },
        getNext:function(orderNumber,type){
            if(type==1){
                location.href="/purchase/myOrderInfo?orderNumber="+orderNumber+"&uid="+this.uid;
            }else if(type==2){
                location.href="/purchase/collageInfo?collageOrderId="+orderNumber;
            }else if(type==3){
                location.href="/purchase/applyRefund?type=2&uid="+this.uid+"&orderNumber="+orderNumber;
            }else if(type==4){
                location.href="/userHomepage?id="+orderNumber;
            }
        },
        getNext1:function(type,item){
            if(type==1){
                location.href="/happyi/commoditySearch?type=3";
            }else if(type==2){
                location.href="/purchase/confirmOrder?uid="+this.uid+"&cartId="+item.orderNumber;
            }

        },
        getLogistics:function(item){
            if(item.state==3){
                vm.$refs.prompt.toast("待商家发货，暂无物流信息！");
            }else{
                location.href="/happyi/refundInfo?type=3&orderNumber="+item.courierNumber+"&uid="+this.uid+"&courier="+encodeURI(item.courier)
            }
        },
        //删除订单
        deleteuserOrder:function(orderNumber){
            var that=this,url,data;
            data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:orderNumber
            };
            this.ajax({url:"/purchase/deleteuserOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.userOrderList();
                }else{
                    vm.$refs.prompt.toast("删除订单失败！");
                }
            });
        },
        //取消订单
        canceluserOrder:function(orderNumber){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:orderNumber
            };
            this.ajax({url:"/purchase/canceluserOrder",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.userOrderList();
                }else{
                    vm.$refs.prompt.toast("取消订单失败！");
                }
            });
        },
        //确认收货
        confirmCollectGoods:function(orderNumber){
            var that=this;
            var data={
                // loginUid:1,
                loginUid:that.uid,
                orderNumber:orderNumber
            };
            this.ajax({url:"/purchase/confirmCollectGoods",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.userOrderList();
                }else{
                    vm.$refs.prompt.toast("确认收货！");
                }
            });
        }
    },
    watch:{
        orderList2:function(){
            var that=this;
            // that.orderList2.forEach(function(val,index){
            //     if(val.state==1){
            //         that.$nextTick(function(){
            //             console.log(index)
            //             var second=getCompareDate(val.currentTime,val.limitTime,"second");
            //             addTimer("stateData"+index,second);
            //         });
            //     }
            // });
        }
    }
});
var addTimer = function () {
    var list = [],
        interval;

    return function (id, time) {
        if (!interval)
            interval = setInterval(go, 1000);
        list.push({ ele: document.getElementById(id), time: time });
    };
    function go() {
        for (var i = 0; i < list.length; i++) {
            list[i].ele.innerHTML= getTimerString(list[i].time ? list[i].time -= 1 : 0);
            if (!list[i].time)
                list.splice(i--, 1);
        }
    }
    function getTimerString(time) {
        h = Math.floor(time/ 3600);
        m = Math.floor((time% 3600) / 60);
        s = Math.floor((time% 3600) % 60);
        if (time>0)
            return checkTime(h) + ":" + checkTime(m) + ":" + checkTime(s);
        else return "00:00:00";
    }
    function checkTime(i) {
        if (i<10) {
            i = "0" +i;
        }
        return i;
    }
} ();
//比较日前大小
function getCompareDate(startTime, endTime, diffType) {
    startTime = JSON.stringify(startTime).split("-").join("/");//replace(/-/g, '/')
    endTime = JSON.stringify(endTime).split("-").join("/");
    //将计算间隔类性字符转换为小写
    diffType = diffType.toLowerCase();
    var sTime =new Date(JSON.parse(startTime)); //开始时间
    var eTime =new Date(JSON.parse(endTime)); //结束时间
    //作为除数的数字
    var timeType =1;
    switch (diffType) {
        case"second":
            timeType =1000;
            break;
        case"minute":
            timeType =1000*60;
            break;
        case"hour":
            timeType =1000*3600;
            break;
        case"day":
            timeType =1000*3600*24;
            break;
        default:
            break;
    }
    return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(timeType));
}
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.orderList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.userOrderList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};